package hzn;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.util.ArrayList;
import java.util.List;

public class Clients {
    String url="hzn100:2181,hzn101:2181";
    int timeout=2000;
    ZooKeeper client;

    void getChildren()throws Exception{
        List<String>children=client.getChildren("/servers",true);
        List<String>hosts=new ArrayList<>();
        for(String c:children){
            byte[]data=client.getData("/servers/"+c,false,null);
            hosts.add(new String(data));
        }
        System.out.println(hosts);
    }
    void getConnect()throws Exception{
        client=new ZooKeeper(url, timeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    getChildren();
                } catch (Exception e) {}
            }
        });
    }

    public static void main(String[] args) throws Exception {
        Clients c=new Clients();
        c.getConnect();
        Thread.sleep(Long.MAX_VALUE);

    }
}
